package com.renwen.sms.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.renwen.sms.dao.CourseDao;
import com.renwen.sms.dao.PingJiaDao;
import com.renwen.sms.dao.TeacherDao;
import com.renwen.sms.dto.PageResult;
import com.renwen.sms.dto.PingJiaDto;
import com.renwen.sms.entity.Course;
import com.renwen.sms.entity.PingJia;
import com.renwen.sms.entity.Teacher;
import com.renwen.sms.service.PingJiaService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class PingJiaServiceImpl implements PingJiaService {
    @Resource
    TeacherDao teacherDao;
    @Resource
    CourseDao courseDao;
    @Resource
    PingJiaDao pingJiaDao;

    /**
     * 查询当前教师所有评价信息
     *
     * @param page      页码
     * @param size      每页显示条数
     * @param searchMap 搜索条件
     * @return 所有评价列表
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public PageResult<PingJiaDto> list(Integer page, Integer size, Map<String, Object> searchMap) {
        QueryWrapper<PingJia> pingJiaQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        // 构建查询条件
        if (!ObjectUtil.hasEmpty(searchMap)) {
            if (!ObjectUtil.hasEmpty(searchMap.get("tNo"))) {
                teacherQueryWrapper.eq("t_no", searchMap.get("tNo"));
            }
        }
        //查询课程号
        teacherQueryWrapper.select("c_no");
        Page<Teacher> teacherPage = teacherDao.selectPage(new Page<Teacher>(page, size), teacherQueryWrapper);
        List<PingJiaDto> cds = new ArrayList<>();
        PingJiaDto pingJiaDto = new PingJiaDto();
        List<PingJia> pingJias = null;
        //遍历当前学生的选课信息
        for (Teacher teacher : teacherPage.getRecords()) {
            if (teacher.getCNo() != null && teacher.getCNo().trim().length() != 0) {
                courseQueryWrapper.eq("c_no", teacher.getCNo());
                Course course = courseDao.selectOne(courseQueryWrapper);
                // 构建查询条件
                if (!ObjectUtil.hasEmpty(searchMap)) {
                    if (!ObjectUtil.hasEmpty(searchMap.get("type"))) {
                        pingJiaQueryWrapper.eq("level", searchMap.get("type"));
                    }
                }
                pingJiaQueryWrapper.eq("c_no", teacher.getCNo());
                pingJias = pingJiaDao.selectList(pingJiaQueryWrapper);

                if (pingJias != null) {
                    for (PingJia pingJia : pingJias) {
                        //拷贝属性
                        BeanUtils.copyProperties(pingJia, pingJiaDto);
                        if (course != null) {
                            pingJiaDto.setCName(course.getCName());
                        }
                        //清空查询条件
                        courseQueryWrapper.clear();
                        pingJiaQueryWrapper.clear();
                        //添加教师信息到数组
                        cds.add(pingJiaDto);
                        //重新开辟内存地址
                        pingJiaDto = new PingJiaDto();
                    }
                }
            }
        }
        return new PageResult<PingJiaDto>(cds, (long) pingJias.size());
    }


}
